<?php
include "templates/header.php";
if ($_POST) {
    if ($_POST['add_new']) {
        $add_fields = array();
        unset($_POST['add_new']);
        foreach ($_POST as $key => $value) {
            $add_fields[$key] = $value;
        }
        if(!$add_fields['show']){
            $add_fields['show'] = 0;
        }
        mysql_insert('price', $add_fields);
    }
    if ($_POST['update_price']) {
        $update_fields = array();
        foreach ($_POST['data'][$_POST['update_price']] as $key => $value) {
            $update_fields[$key] = $value;
        }
        if(!$update_fields['show']){
            $update_fields['show'] = 0;
        }
        mysql_update('price', $update_fields, $_POST['update_price']);
    }
}
$products = mysql_select_array("SELECT * FROM `product`");
$pricelist = mysql_select_array("SELECT * FROM `price` ORDER BY `title` ASC");
?>

<div style="width: 100%; height: auto; margin: 10px auto; text-align: center;">
    <table border="1" cellspacing="0" style="width: 100%; text-align: center;">
        <tr>
            <td>Наименование</td>
            <td style="width: 100px;">Цена, грн.</td>
            <td style="width: 100px;">Единицы</td>
            <td style="width: 200px;">Связанный продукт</td>
            <td>Минимальный заказ</td>
            <td style="width: 75px;">Показать</td>
            <td style="width: 100px;">&nbsp;</td>
        </tr>
        <tr>
        <form method="post" action="">
            <input type="hidden" name="add_new" value="1" />
            <td><input type="text" style="width: 100%;" name="title" /></td>
            <td><input type="text" style="width: 100%;" name="price" /></td>
            <td><input type="text" style="width: 100%;" name="measure" /></td>
            <td>
                <select name="related_product" style="width: 100%;">
                    <option value="0">Выберите продукт</option>
                    <? foreach ($products as $key => $product): ?>
                        <option value="<?= $product['id']; ?>"><?= $product['title']; ?></option>
                    <? endforeach; ?>
                    </select>
                </td>
                <td><input type="text" style="width: 100%;" name="min" /></td>
                <td><input type="checkbox" name="show" value="1" /></td>
                <td><input type="submit" style="width: 100%;" value="Добавить" /></td>
            </form>
            </tr>
            <? foreach ($pricelist as $key => $price): ?>
            <tr>
        <form method="post" action="">
            <input type="hidden" name="update_price" value="<?=$price['id'];?>" />
            <td><input type="text" style="width: 100%;" name="data[<?=$price['id'];?>][title]" value="<?=$price['title'];?>" /></td>
            <td><input type="text" style="width: 100%;" name="data[<?=$price['id'];?>][price]" value="<?=$price['price'];?>" /></td>
            <td><input type="text" style="width: 100%;" name="data[<?=$price['id'];?>][measure]" value="<?=$price['measure'];?>" /></td>
            <td>
                <select name="data[<?=$price['id'];?>][related_product]" style="width: 100%;">
                    <option value="0">Выберите продукт</option>
                    <? foreach ($products as $key => $product): ?>
                        <option value="<?= $product['id']; ?>"  <?if($price['related_product'] == $product['id']):?>selected="selected"<?endif;?>><?= $product['title']; ?></option>
                    <? endforeach; ?>
                    </select>
                </td>
                <td><input type="text" style="width: 100%;" name="data[<?=$price['id'];?>][min]" value="<?=$price['min'];?>" /></td>
                <td><input type="checkbox" name="data[<?=$price['id'];?>][show]" value="1" <?if($price['show'] == 1):?>checked="checked"<?endif;?> /></td>
                <td><input type="submit" style="width: 100%;" value="Обновить" /></td>
            </form>
            </tr>
            <? endforeach; ?>
        </table>
    </div>
<? include "templates/footer.php"; ?>